package com.example.moshudriver.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.BeeFramework.view.ToastView;
import com.example.moshudriver.tools.Config;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.sgr.dykdriver.R;
import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMessage;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.BaseRequest;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboDownloadListener;
import com.sina.weibo.sdk.api.share.IWeiboHandler.Request;
import com.sina.weibo.sdk.api.share.IWeiboHandler.Response;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.constant.WBConstants;
import com.sina.weibo.sdk.utils.Utility;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
//微信分享
@SuppressLint("NewApi")
public class WXEntryActivity extends Activity implements OnClickListener,
		Request, Response, IWXAPIEventHandler {

	private LinearLayout share_weixin;
	private LinearLayout share_friend;

	private Button share_cancel;

	private LinearLayout share_friendline_comment;
	private LinearLayout share_wx_comment;

	private Button share_cancel_comment;
	private IWeiboShareAPI mWeiboShareAPI;
	private Bitmap detaultShareImage = null;
	private Bitmap shareImage = null;

	private IWXAPI weixinAPI = null;
	private Tencent mTencent;
	// QZone分享， SHARE_TO_QQ_TYPE_DEFAULT 图文，SHARE_TO_QQ_TYPE_IMAGE 纯图
	private int shareType = QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT;
	public static final String IS_COMMENT = "is_comment";
	private View contentView;
	private String invitecode;
	public static final String INVITECODE = "invitecode";
	private SharedPreferences shared;
	private int order_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		shared = getSharedPreferences(O2OMobileAppConst.USERINFO, 0);
		if (getIntent().getBooleanExtra(IS_COMMENT, false)) {
			contentView = getLayoutInflater().inflate(R.layout.share_comment,
					null);
			setContentView(contentView);
			order_id = getIntent().getIntExtra("order_id", 0);
		
//			mTencent = Tencent.createInstance(Config.QQZone_API_ID,
//					WXEntryActivity.this.getApplicationContext());
			share_friendline_comment = (LinearLayout) findViewById(R.id.share_friendline_comment);
			share_wx_comment = (LinearLayout) findViewById(R.id.share_sina_comment);
			
			share_cancel_comment = (Button) findViewById(R.id.share_cancel_comment);
			share_friendline_comment.setOnClickListener(this);
			share_wx_comment.setOnClickListener(this);
			
			share_cancel_comment.setOnClickListener(this);
		} else {
			contentView = getLayoutInflater()
					.inflate(R.layout.share_view, null);
			setContentView(contentView);
			invitecode = getIntent().getStringExtra(INVITECODE);
		
			share_weixin = (LinearLayout) findViewById(R.id.share_weixin);
			share_friend = (LinearLayout) findViewById(R.id.share_friend);
			
			share_cancel = (Button) findViewById(R.id.share_cancel);

			share_weixin.setOnClickListener(this);
			share_friend.setOnClickListener(this);
			share_cancel.setOnClickListener(this);
		}
		android.view.ViewGroup.LayoutParams cursorParams = (android.view.ViewGroup.LayoutParams) contentView
				.getLayoutParams();
		cursorParams.width = getResources().getDisplayMetrics().widthPixels;
		contentView.setLayoutParams(cursorParams);
		contentView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			}
		});
		// 默认分享的图标
		detaultShareImage = BitmapFactory.decodeResource(getResources(),
				R.drawable.logo);
//		  Intent intent=getIntent();
//		  detaultShareImage=intent.getParcelableExtra(WXEntryActivity.INVITECODE);
		if (shareImage == null) {
			shareImage = detaultShareImage;
		}
		mWeiboShareAPI = WeiboShareSDK
				.createWeiboAPI(this, Config.SINA_APP_KEY);
		 // NOTE：请务必提前注册，即界面初始化的时候或是应用程序初始化时，进行注册
        mWeiboShareAPI.registerApp();
		if (!mWeiboShareAPI.isWeiboAppInstalled()) {
			mWeiboShareAPI
					.registerWeiboDownloadListener(new IWeiboDownloadListener() {
						@Override
						public void onCancel() {
							Toast.makeText(WXEntryActivity.this, "取消下载",
									Toast.LENGTH_SHORT).show();
						}
					});
		}
		if (savedInstanceState != null) {
			mWeiboShareAPI.handleWeiboRequest(getIntent(), this);
		}

		if (null == weixinAPI) {
			weixinAPI = WXAPIFactory.createWXAPI(this, Config.WEIXIN_APP_ID,
					true);
			weixinAPI.registerApp(Config.WEIXIN_APP_ID);
		}
		weixinAPI.handleIntent(getIntent(), this);

	}

	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		// 从当前应用唤起微博并进行分享后，返回到当前应用时，需要在此处调用该函数
		mWeiboShareAPI.handleWeiboResponse(intent, this);
		weixinAPI.handleIntent(intent, this);
	}

	@Override
	public void onClick(View v) {
		String shareContent = getString(R.string.share_content)+invitecode;
		System.out.println(getString(R.string.share_content)+invitecode);
		// TODO Auto-generated method stub
		switch (v.getId()) {
	/*	case R.id.share_sina:
			if (mWeiboShareAPI.checkEnvironment(true)) {
				mWeiboShareAPI.registerApp();
			}
			sendMessage(shareContent, "http://didame.com/test/verify?invite_code="+invitecode);
			break;*/
		case R.id.share_weixin:
			shareToWeiXin(shareContent, "http://didame.com/test/verify?invite_code="+invitecode);
			break;
		case R.id.share_friend:
			shareToFriendLine(shareContent, "http://didame.com/test/verify?invite_code="+invitecode);
			break;
		/*case R.id.share_QQ:
//			ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
//			clipboard.setText(shareContent);
//			ToastView toast = new ToastView(WXEntryActivity.this, "已复制到粘贴板");
//			toast.setGravity(Gravity.CENTER, 0, 0);
//			toast.show();
//			if (mTencent.isReady()) {
//
//			}
//			Toast.makeText(WXEntryActivity.this, "QQ分享", Toast.LENGTH_SHORT)
//					.show();
//			ArrayList<String> Urls = new ArrayList<String>();
			final Bundle params1 = new Bundle();
			params1.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
					QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
			params1.putString(QQShare.SHARE_TO_QQ_TITLE, "摩叔之家");
			params1.putString(QQShare.SHARE_TO_QQ_SUMMARY, "摩叔注册邀请");
			params1.putString(QQShare.SHARE_TO_QQ_TARGET_URL,
					"http://didame.com/test/verify?invite_code="+invitecode);
			params1.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,
					"http://didame.com/picture/logo.png");
			params1.putString(QQShare.SHARE_TO_QQ_APP_NAME, "摩叔");
//			params1.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, Urls);
			doShareToQQ(params1);
			finish();
			overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			break;*/
		case R.id.share_cancel:
			finish();
			overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			break;
		case R.id.share_cancel_comment:
			finish();
			overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			break;
		case R.id.share_friendline_comment:
			shareToFriendLine(shareContent, "http://didame.com/test/verify?invite_code="+invitecode);
			break;
		case R.id.share_sina_comment://微信分享
			shareToWeiXin(shareContent, "http://didame.com/test/verify?invite_code="+invitecode);
			break;
/*		case R.id.share_qzone_comment:
//			if (mTencent.isReady()) {
//
//			}
			ArrayList<String> imageUrls = new ArrayList<String>();
			final Bundle params = new Bundle();
			params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, shareType);
			params.putString(QzoneShare.SHARE_TO_QQ_TITLE,
					getString(R.string.share_title));
			params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "摩叔注册邀请");
			params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,
					"http://didame.com/test/verify?invite_code="+invitecode);
			//摩叔图标的url
			imageUrls
					.add("http://didame.com/picture/logo.png");
			params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,
					imageUrls);
			doShareToQzone(params);
			break;*/
		}
	}

	@Override
	public void onRequest(BaseRequest arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * 第三方应用发送请求消息到微博，唤起微博分享界面。
	 * 
	 * @see {@link #sendMultiMessage} 或者 {@link #sendSingleMessage}
	 */
	private void sendMessage(String shareTitle, String shareUrl) {

		if (mWeiboShareAPI.isWeiboAppSupportAPI()) {
			int supportApi = mWeiboShareAPI.getWeiboAppSupportAPI();
			if (supportApi >= 10351 /* ApiUtils.BUILD_INT_VER_2_2 */) {
				sendMultiMessage(shareTitle, shareUrl);
			} else {
				sendSingleMessage(shareTitle, shareUrl);
			}
		} else {
			// Toast.makeText(this, R.string.weibosdk_demo_not_support_api_hint,
			// Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 第三方应用发送请求消息到微博，唤起微博分享界面。 当
	 * {@link com.sina.weibo.sdk.api.share.IWeiboShareAPI#getWeiboAppSupportAPI()}
	 * < 10351 时，只支持分享单条消息，即 文本、图片、网页、音乐、视频中的一种，不支持Voice消息。
	 */
	private void sendSingleMessage(String shareTitle, String shareUrl) {
		// 1. 初始化微博的分享消息
		// 用户可以分享文本、图片、网页、音乐、视频中的一种
		WeiboMessage weiboMessage = new WeiboMessage();
		if (null != shareTitle) {
			weiboMessage.mediaObject = getTextObj(shareTitle);
		}

		if (null != shareUrl) {
			weiboMessage.mediaObject = getWebpageObj(shareTitle, shareUrl);
		}

		// 2. 初始化从第三方到微博的消息请求
		SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
		// 用transaction唯一标识一个请求
		request.transaction = String.valueOf(System.currentTimeMillis());
		request.message = weiboMessage;

		// 3. 发送请求消息到微博，唤起微博分享界面
		mWeiboShareAPI.sendRequest(request);
	}

	private void sendMultiMessage(String shareTitle, String shareUrl) {
		// 1. 初始化微博的分享消息
		WeiboMultiMessage weiboMessage = new WeiboMultiMessage();

		// 用户可以分享其它媒体资源（网页、音乐、视频、声音中的一种）
		if (null != shareUrl) {
			weiboMessage.mediaObject = getWebpageObj(shareTitle, shareUrl);
		} else if (null != shareTitle) {
			weiboMessage.textObject = getTextObj(shareTitle);
		}
		if (null != shareImage) {
			weiboMessage.imageObject = getImageObj();
		}
		// 2. 初始化从第三方到微博的消息请求
		SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
		// 用transaction唯一标识一个请求
		request.transaction = String.valueOf(System.currentTimeMillis());
		request.multiMessage = weiboMessage;

		// 3. 发送请求消息到微博，唤起微博分享界面
		mWeiboShareAPI.sendRequest(request);
	}

	/**
	 * 创建文本消息对象。
	 * 
	 * @return 文本消息对象。
	 */
	private TextObject getTextObj(String shareText) {
		TextObject textObject = new TextObject();
		textObject.text = shareText;
		return textObject;
	}

	/**
	 * 创建图片消息对象。
	 * 
	 * @return 图片消息对象。
	 */
	private ImageObject getImageObj() {
		ImageObject imageObject = new ImageObject();
		imageObject.setImageObject(shareImage);
		return imageObject;
	}

	/**
	 * 创建多媒体（网页）消息对象。
	 * 
	 * @return 多媒体（网页）消息对象。
	 */
	private WebpageObject getWebpageObj(String title, String shareUrl) {
		WebpageObject mediaObject = new WebpageObject();
		mediaObject.identify = Utility.generateGUID();
		// 设置 Bitmap 类型的图片到视频对象里
		if (null != shareImage) {
			mediaObject.setThumbImage(shareImage);
		} else {
			mediaObject.setThumbImage(detaultShareImage);
		}
		mediaObject.title = "递一快";
		mediaObject.actionUrl = shareUrl;
		mediaObject.defaultText = title;
		mediaObject.description = title;
		return mediaObject;
	}

	/**
	 * 新浪微博返回状态
	 */
	@Override
	public void onResponse(BaseResponse baseResp) {
		ToastView toast;
		switch (baseResp.errCode) {
		case WBConstants.ErrorCode.ERR_OK:
			toast = new ToastView(WXEntryActivity.this, "分享成功");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		case WBConstants.ErrorCode.ERR_CANCEL:
			toast = new ToastView(WXEntryActivity.this, "取消分享");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		case WBConstants.ErrorCode.ERR_FAIL:
			toast = new ToastView(WXEntryActivity.this, "分享失败"
					+ "Error Message: " + baseResp.errMsg);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		}
		finish();
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	@Override
	public void onReq(BaseReq arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * 微信返回状态
	 */
	@Override
	public void onResp(BaseResp baseResp) {
		ToastView toast;
		switch (baseResp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			toast = new ToastView(WXEntryActivity.this, "发送成功");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			toast = new ToastView(WXEntryActivity.this, "取消发送");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			toast = new ToastView(WXEntryActivity.this, "发送被拒绝");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		default:
			toast = new ToastView(WXEntryActivity.this, "发送返回");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;
		}
		finish();
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	/**
	 * 分享到微信
	 * 
	 * @param title
	 *            标题
	 * @param urlContent
	 *            链接
	 */
	public void shareToWeiXin(String title, String urlContent) {
		if (weixinAPI.isWXAppInstalled()) {
			WXWebpageObject webpage = new WXWebpageObject();
			webpage.webpageUrl = urlContent;
			WXMediaMessage msg = new WXMediaMessage(webpage);

			msg.title = title;

			msg.description = title;

			if (null != shareImage) {
				msg.setThumbImage(shareImage);
			} else {
				msg.setThumbImage(detaultShareImage);
			}

			msg.description = title;

			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("img");
			req.message = msg;
			req.scene = SendMessageToWX.Req.WXSceneSession;

			weixinAPI.sendReq(req);
		} else {
			Toast.makeText(this, "未安装微信客户端", Toast.LENGTH_LONG).show();
		}
	}

	public void shareToFriendLine(String title, String urlContent) {
		if (weixinAPI.isWXAppInstalled()) {
			WXWebpageObject webpage = new WXWebpageObject();
			webpage.webpageUrl = urlContent;
			WXMediaMessage msg = new WXMediaMessage(webpage);
			msg.title = getString(R.string.share_title);

			msg.description = getString(R.string.share_title);
			if (null != shareImage) {
				msg.setThumbImage(shareImage);
			} else {
				msg.setThumbImage(detaultShareImage);
			}

			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("img");
			req.message = msg;
			req.scene = SendMessageToWX.Req.WXSceneTimeline;

			weixinAPI.sendReq(req);
		} else {
			Toast.makeText(this, "未安装微信客户端", Toast.LENGTH_LONG).show();
		}
	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			return false;
		}
		return true;
	}

	/**
	 * 用异步方式启动分享到QQ		
	 * 
	 * @param params
	 */
	private void doShareToQQ(final Bundle params) {
		mTencent = Tencent.createInstance(Config.QQZone_API_ID,
				WXEntryActivity.this.getApplicationContext());
		final Activity activity = WXEntryActivity.this;
		// TODO Auto-generated method stub
		System.out.println("mTencent"+mTencent+"activity:"+activity+"params："+params);
		mTencent.shareToQQ(activity, params, new IUiListener() {

			@Override
			public void onError(UiError arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(WXEntryActivity.this, "发送失败", Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onComplete(Object arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(WXEntryActivity.this, "发送完成", Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onCancel() {
				// TODO Auto-generated method stub
//				Toast.makeText(WXEntryActivity.this, "发送取消", Toast.LENGTH_SHORT)
//						.show();
			}
		});
	}
	
	/**
	 * 用异步方式启动分享
	 * 
	 * @param params
	 */
	private void doShareToQzone(final Bundle params) {
		mTencent = Tencent.createInstance(Config.QQZone_API_ID,
				WXEntryActivity.this.getApplicationContext());
		final Activity activity = WXEntryActivity.this;
		// TODO Auto-generated method stub
		System.out.println("mTencent"+mTencent+"activity:"+activity+"params："+params);
		mTencent.shareToQzone(activity, params, new IUiListener() {

			@Override
			public void onCancel() {
//				ToastView toast = new ToastView(WXEntryActivity.this, "发送取消");
//				toast.setGravity(Gravity.CENTER, 0, 0);
//				toast.show();
			}

			@Override
			public void onError(UiError e) {
				ToastView toast = new ToastView(WXEntryActivity.this, "发送失败");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}

			@Override
			public void onComplete(Object response) {
				// TODO Auto-generated method stub
				ToastView toast = new ToastView(WXEntryActivity.this, "发送成功");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}

		});
	}

}
